﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Twi.COMMON.Core
{
    /**
     * Author:feitianxinhong
     * Create Date:2015-03-23
     * Description:默认值扩展方法
     */
    public static class ExtensionDefaultValue
    {
        /// <summary>
        /// 字符串默认值
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>如果为空，返回默认值，否则返回自身</returns>
        public static string SelfOrDefault(this string str, string defaultValue) {
            return string.IsNullOrEmpty(str) ? defaultValue : str;
        }

        /// <summary>
        /// 字典指定键默认值
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>如果键不存在，返回默认值，否则返回键对应的值</returns>
        public static dynamic SelfOrDefault(this Dictionary<string,dynamic> dict,string key, dynamic defaultValue)
        {
            if (dict !=null && dict.ContainsKey(key))
            {
                return dict[key];
            }
            else {
                return defaultValue;
            }
        }
    }
}
